package com.chinacoal.microservice.impl.service.impl;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.chinacoal.microservice.api.model.ForeignTaskParam;
import com.chinacoal.microservice.api.model.entity.UtaskInfoEntity;
import com.chinacoal.microservice.api.model.resource.BusinessData;
import com.chinacoal.microservice.api.model.resource.DictionaryCanvasData;
import com.chinacoal.microservice.api.model.resource.FormCanvasData;
import com.chinacoal.microservice.api.model.resource.OaBusinessData;
import com.chinacoal.microservice.api.model.resource.RecordData;
import com.chinacoal.microservice.api.model.resource.TableCanvasData;
import com.chinacoal.microservice.impl.service.OaTaskService;
import com.chinacoal.microservice.impl.service.UtaskInfoService;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;
import com.chinacoal.microservice.util.tool.Tool;

/**
 * 
 * 描述 : 集团慧点OA系统待办集成ServiceImpl
 * 创建人 : yn
 * 创建时间 : 2020年5月8日 下午1:22:30
 * 版本 : 1.0
 * 包名 : com.chinacoal.microservice.impl.service.impl
 * 类名 : OaTaskServiceImpl
 */
@Service
public class OaTaskServiceImpl implements OaTaskService {
	
	/**
	 * logger日志
	 */
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 代办Service
	 */
	@Resource
	private UtaskInfoService utaskInfoService;
	
    /**
     * OA系统待办地址
     */
    @Value("${oaTask.flowOper.wsdl}")
    private String flowOperPath;
    
    /**
     * OA系统下载附件地址
     */
    @Value("${oaTask.downAttach.wsdl}")
    private String downAttachPath;

	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月8日 下午1:22:48
	 * 描述 : OA系统“代办详情”
	 * 包名 : com.chinacoal.microservice.impl.service.impl
	 * 方法名 : getTaskDetails
	 *  Result<BusinessData>  
	 *  @throws
	 */
	@Override
	public Result<BusinessData> getTaskDetails(ForeignTaskParam foreignTaskParam) throws Exception {
		OaBusinessData businessData = new OaBusinessData(); // 创建OA系统XML数据转换对象类
//		UtaskInfoEntity utaskInfoEntity = utaskInfoService.getTask(foreignTaskParam.getSysCode(), foreignTaskParam.getBusinessId());
		UtaskInfoEntity utaskInfoEntity = utaskInfoService.getUtaskInfoById(foreignTaskParam.getTaskId()) ;
		if (utaskInfoEntity == null) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("获取代办详情失败,请确认！"));
		}
		String oaDataPath = ""; // 数据库路径
		if ("xxgssw01".equals(foreignTaskParam.getBizCode())) {
			oaDataPath = "kfgs/dep400/swgl_400.nsf";
		} else if ("xxgsfw01".equals(foreignTaskParam.getBizCode())) {
			oaDataPath = "kfgs/dep400/fwgl_400.nsf";
		}
		String oaDataStatus = "2"; // 数据状态
		String oaUserAccount = utaskInfoEntity.getHandleUserId(); // 用户账号
		String oaSubmit = foreignTaskParam.getExtParams() != null && foreignTaskParam.getExtParams().containsKey("oaSubmit")
				? (String) foreignTaskParam.getExtParams().get("oaSubmit")
				: ""; // 提交
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:FLOWOPER>";
			paramXml += 			"<urn:UNID>" + foreignTaskParam.getBusinessId() + "</urn:UNID>"; // 待办ID
			paramXml += 			"<urn:DATAPATH>" + oaDataPath + "</urn:DATAPATH>"; // 数据库路径
			paramXml += 			"<urn:RECODE>" + oaUserAccount + "</urn:RECODE>"; // 用户账号
			paramXml += 			"<urn:DBMARK>" + oaDataStatus + "</urn:DBMARK>"; // 数据状态
			paramXml += 			"<urn:SUBMIT>" + oaSubmit + "</urn:SUBMIT>"; // 提交
			paramXml += 		"</urn:FLOWOPER>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(flowOperPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "FLOWOPERReturn"); // OA系统代办明细Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断代办明细Xml是否为空
					// 如果需要在URL中用到，需要将这些特殊字符换成相应的十六进制的值
					resultXml = resultXml.replaceAll("&", "%26");
					Document doc = DocumentHelper.parseText(resultXml);
					Element root = doc.getRootElement();
					
					// 1、基础信息
					if (root.element("data").element("baseInfo") != null) {
						FormCanvasData formCanvasData = new FormCanvasData();
						Element baseInfoElement = root.element("data").element("baseInfo");
						for (Iterator<Element> iterable = baseInfoElement.elementIterator(); iterable.hasNext();) {
							Element nodeElement = iterable.next();
							formCanvasData.add(nodeElement.attributeValue("name"), nodeElement.attributeValue("value"));
						}
						formCanvasData.setName("信息公司收发文");
						businessData.addFormCanvasData(formCanvasData);
					}
					
					// 2、附件信息
					if (root.element("data").element("attinfo") != null) {
						TableCanvasData fileInfo = new TableCanvasData();
						Element fileInfoElement = root.element("data").element("attinfo");
						for (Iterator<Element> appopinions = fileInfoElement.elementIterator(); appopinions.hasNext();) {
							Element nodeElement = appopinions.next();
							RecordData advise = new RecordData();
							/** <element url="http://oa3.chinacoal.com" name="附件名" value="国胜同志批示.docx"  filesize="51094"  order="19" /> **/
							String url = nodeElement.attributeValue("url"); // 附件地址
							String name = nodeElement.attributeValue("name"); // 附件名称
							String value = nodeElement.attributeValue("value"); // 附件名称值
							String filesize = nodeElement.attributeValue("filesize"); // 附件大小
							advise.add("name", name, value);
							advise.add("url", "附件地址",
									"/uniontask/foreign/task/attach?sysCode=SYS-20200325-1003&fileName="
											+ URLEncoder.encode(value, "UTF-8") + "&filePath="
											+ URLEncoder.encode(url, "UTF-8") + "&sysTaskId=");
							advise.add("filesize", "附件大小", filesize);
							fileInfo.add(advise);
						}
						businessData.setFileInfo(fileInfo);
					}
					
					// 3、审核信息
					if (root.element("data").element("appopinions") != null) {
						TableCanvasData approvalRecords = new TableCanvasData();
						Element adviseInfoElement = root.element("data").element("appopinions");
						for (Iterator<Element> appopinions = adviseInfoElement.elementIterator(); appopinions.hasNext();) {
							RecordData advise = new RecordData();
							Element adviseElement = appopinions.next();
							for (Iterator<Element> adviseIterable = adviseElement.elementIterator(); adviseIterable.hasNext();) {
								Element nodeElement = adviseIterable.next();
								/** <record>
									<element url="" name="处理人" value="姚恒" personid="yaoheng" />
									<element url="" name="处理时间" value="2020-05-06 10:35:29"/>
									<element url="" name="处理状态" value="提出拟办意见"/>
									<element url="base64" name="意见" value="6K+35Zac5Y2H5ZCM5b+X6ZiF56S677yB"/>
								</record> **/
								String name = nodeElement.attributeValue("name");
								String value = nodeElement.attributeValue("value");
								String id = "";
								if (StringUtils.isNoneBlank(name)) {
									switch (name) {
									case "处理人":
										id = "handler";
										break;
									case "处理时间":
										id = "handlerDate";
										break;
									case "处理状态":
										id = "handlerStatus";
										break;
									case "意见":
										id = "opinion";
										if (StringUtils.isNoneBlank(value)) {
											value = value.replaceAll(" ", "");
											value = new String(Base64.getDecoder().decode(value), "utf-8");
										}
										break;
									default:
										break;
									}
								}
								advise.add(id, name, value);
							}
							approvalRecords.add(advise);
						}
						businessData.setApprovalRecords(approvalRecords);
					}
					
					// 4、审核按钮
					if (root.element("data").element("bottoninfo") != null) {
						Element buttonInfoElement = root.element("data").element("bottoninfo");
						for (Iterator<Element> resultInfoIterable = buttonInfoElement.elementIterator(); resultInfoIterable.hasNext();) {
							Element resultElement = resultInfoIterable.next();
							/** <bottoninfo>
									<element url="" name="提交" value="0" parent="1" />
									<element url="" name="驳回" value="1" parent="2" />
									<element url="" name="会签" value="1" parent="3" />
									<element url="" name="会签提交" value="1" parent="4" />
									<element url="xxx" name="归档" value="1" parent="6" />
									<element url="xxx" name="发文" value="1" parent="0" />
									<element url="xxx" name="返回原系统" value="0" parent="0" />
									<element url="xxx" name="编号" value="1" parent="7" />
									<element url="xxx" name="套打" value="1" parent="8" />
									<element url="xxx" name="盖章" value="1" parent="9" />
									<element url="xxx" name="流程图" value="0" />
									<element url="" name="显示下环节信息" value="0" />
								</bottoninfo>
							**/
							String url = resultElement.attributeValue("url");
							String name = resultElement.attributeValue("name");
							String value = resultElement.attributeValue("value");
							String parent = resultElement.attributeValue("parent");
							if ("显示下环节信息".equals(name)) { // 值为0时，提交时需要显示下环节信息； 值为1时，则不需要显示下环节信息；
								businessData.initButtonExt().add(name, value);
								continue;
							}
							if ("1".equals(value)) { // 按钮value值：0-显示，1-不显示；
								continue;
							}
							if ("mobile".equals(foreignTaskParam.getReqTerminal())) { // 判断请求来源终端类型等于mobile时
								if ("归档".equals(name) || 
									"返回原系统".equals(name) || 
									"编号".equals(name) || 
									"流程图".equals(name) ||
									"套打".equals(name) || 
									"盖章".equals(name) || 
									"发文".equals(name)) {
									continue;
								}
								businessData.initButtonInfo().add(name, parent);
							} else {
								businessData.initButtonInfo().add(name, StringUtils.isNoneBlank(parent) ? parent : url);
							}
							
						}
					}
					
					// 5、返回结果集
					if (root.element("data").element("result") != null) {
						DictionaryCanvasData resultInfo = new DictionaryCanvasData();
						Element resultInfoElement = root.element("data").element("result");
						for (Iterator<Element> iterable = resultInfoElement.elementIterator(); iterable.hasNext();) {
							Element nodeElement = iterable.next();
							resultInfo.add(nodeElement.attributeValue("name"), nodeElement.attributeValue("value"));
						}
						businessData.setResultInfo(resultInfo);
					}
					
					// 6、是否填写意见
					if (root.element("data").element("datainfo") != null) {
						Element datainfoElement = root.element("data").element("datainfo");
						for (Iterator<Element> iterable = datainfoElement.elementIterator(); iterable.hasNext();) {
							Element nodeElement = iterable.next();
							businessData.initDataInfo().add(nodeElement.attributeValue("name"), nodeElement.attributeValue("value"));
						}
					}
					
					// 7、常用语
					if (root.element("data").element("userOpin") != null) {
						Element userOpinElement = root.element("data").element("userOpin");
						for (Iterator<Element> iterable = userOpinElement.elementIterator(); iterable.hasNext();) {
							Element nodeElement = iterable.next();
							businessData.initUserOpin().add(nodeElement.attributeValue("name"), nodeElement.attributeValue("value"));
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、getTaskDetails；报错内容、" + e.getMessage());
			return Result.error(CodeMsg.SERVER_ERROR.fillArgs(e.getMessage()));
		}
		return Result.success(businessData);
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 上午10:54:58
	 * 描述 : OA系统“下一环节”
	 * 包名 : com.chinacoal.microservice.impl.service.impl
	 * 方法名 : taskNextNode
	 *  Result<Object>  
	 *  @throws
	 */
	public Result<Object> taskNextNode(Map<String, Object> param) throws Exception {
		String oaUnId = (String) param.get("oaUnId"); // 待办ID
		if (StringUtils.isBlank(oaUnId)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“待办ID”参数！"));
		}
		String oaDataPath = (String) param.get("oaDataPath"); // 数据库路径
		if (StringUtils.isBlank(oaDataPath)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“数据库路径”参数！"));
		}
		String oaOpIn = (String) param.get("oaOpIn"); // 预留字段
		String oaUserAccount = (String) param.get("oaUserAccount"); // 用户账号
		if (StringUtils.isBlank(oaUserAccount)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“用户账号”参数！"));
		}
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:WSFORGETNEXTNODE>";
			paramXml += 			"<urn:UNID>" + oaUnId + "</urn:UNID>"; // 待办ID
			paramXml += 			"<urn:DATAPATH>" + oaDataPath + "</urn:DATAPATH>"; // 数据库路径
			paramXml += 			"<urn:OPIN>" + oaOpIn + "</urn:OPIN>"; // 预留字段
			paramXml += 			"<urn:USER>" + oaUserAccount + "</urn:USER>"; // 用户账号
			paramXml += 		"</urn:WSFORGETNEXTNODE>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(flowOperPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "WSFORGETNEXTNODEReturn"); // OA系统下一环节Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断下一环节Xml是否为空
					System.out.println(resultXml);
					/** Document doc = DocumentHelper.parseText(resultXml);
					Element root = doc.getRootElement(); **/
				}
			}
		} catch (Exception e) {
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、taskNextNode；报错内容、" + e.getMessage());
			return Result.error(CodeMsg.SERVER_ERROR.fillArgs(e.getMessage()));
		}
		return null;
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 下午2:04:40
	 * 描述 : OA系统“驳回”
	 * 包名 : com.chinacoal.microservice.impl.service
	 * 方法名 : taskReject
	 *  Result<Object>  
	 *  @throws
	 */
	public Result<Object> taskReject(Map<String, Object> param) throws Exception {
		String oaUnId = (String) param.get("oaUnId"); // 待办ID
		if (StringUtils.isBlank(oaUnId)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“待办ID”参数！"));
		}
		String oaDataPath = (String) param.get("oaDataPath"); // 数据库路径
		if (StringUtils.isBlank(oaDataPath)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“数据库路径”参数！"));
		}
		String oaUserAccount = (String) param.get("oaUserAccount"); // 用户账号
		if (StringUtils.isBlank(oaUserAccount)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“用户账号”参数！"));
		}
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:WSFORDENY>";
			paramXml += 			"<urn:UNID>" + oaUnId + "</urn:UNID>"; // 待办ID
			paramXml += 			"<urn:DATAPATH>" + oaDataPath + "</urn:DATAPATH>"; // 数据库路径
			paramXml += 			"<urn:USER>" + oaUserAccount + "</urn:USER>"; // 用户账号
			paramXml += 		"</urn:WSFORDENY>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(flowOperPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "WSFORDENYReturn"); // OA系统驳回Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断下一环节Xml是否为空
					System.out.println(resultXml);
					/** Document doc = DocumentHelper.parseText(resultXml);
					Element root = doc.getRootElement(); **/
				}
			}
		} catch (Exception e) {
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、taskReject；报错内容、" + e.getMessage());
			return Result.error(CodeMsg.SERVER_ERROR.fillArgs(e.getMessage()));
		}
		return null;
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 下午2:05:34
	 * 描述 : OA系统“会签”
	 * 包名 : com.chinacoal.microservice.impl.service
	 * 方法名 : taskCountersign
	 *  Result<Object>  
	 *  @throws
	 */
	public Result<Object> taskCountersign(Map<String, Object> param) throws Exception {
		String oaUnId = (String) param.get("oaUnId"); // 待办ID
		if (StringUtils.isBlank(oaUnId)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“待办ID”参数！"));
		}
		String oaDataPath = (String) param.get("oaDataPath"); // 数据库路径
		if (StringUtils.isBlank(oaDataPath)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“数据库路径”参数！"));
		}
		String oaUserAccount = (String) param.get("oaUserAccount"); // 用户账号
		if (StringUtils.isBlank(oaUserAccount)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“用户账号”参数！"));
		}
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:HUIQIANSELSECT>";
			paramXml += 			"<urn:UNID>" + oaUnId + "</urn:UNID>"; // 待办ID
			paramXml += 			"<urn:DATAPATH>" + oaDataPath + "</urn:DATAPATH>"; // 数据库路径
			paramXml += 			"<urn:CURUSER>" + oaUserAccount + "</urn:CURUSER>"; // 用户账号
			paramXml += 		"</urn:HUIQIANSELSECT>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(flowOperPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "HUIQIANSELSECTReturn"); // OA系统会签Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断下一环节Xml是否为空
					System.out.println(resultXml);
					/** Document doc = DocumentHelper.parseText(resultXml);
					Element root = doc.getRootElement(); **/
				}
			}
		} catch (Exception e) {
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、taskCountersign；报错内容、" + e.getMessage());
			return Result.error(CodeMsg.SERVER_ERROR.fillArgs(e.getMessage()));
		}
		return null;
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 下午2:07:10
	 * 描述 : OA系统“接收”
	 * 包名 : com.chinacoal.microservice.impl.service
	 * 方法名 : taskAccept
	 *  Result<Object>  
	 *  @throws
	 */
	public Result<Object> taskAccept(Map<String, Object> param) throws Exception {
		String oaUnId = (String) param.get("oaUnId"); // 待办ID
		if (StringUtils.isBlank(oaUnId)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“待办ID”参数！"));
		}
		String oaDataPath = (String) param.get("oaDataPath"); // 数据库路径
		if (StringUtils.isBlank(oaDataPath)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“数据库路径”参数！"));
		}
		String oaUserAccount = (String) param.get("oaUserAccount"); // 用户账号
		if (StringUtils.isBlank(oaUserAccount)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“用户账号”参数！"));
		}
		String oaGwlx = (String) param.get("oaGwlx"); // 公文类型
		if (StringUtils.isBlank(oaGwlx)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“公文类型”参数！"));
		}
		String oaExt = (String) param.get("oaExt"); // 预留字段
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:RESAVEGW>";
			paramXml += 			"<urn:UNID>" + oaUnId + "</urn:UNID>"; // 待办ID
			paramXml += 			"<urn:DATAPATH>" + oaDataPath + "</urn:DATAPATH>"; // 数据库路径
			paramXml += 			"<urn:CURUSER>" + oaUserAccount + "</urn:CURUSER>"; // 用户账号
			paramXml += 			"<urn:GWLX>" + oaGwlx + "</urn:GWLX>"; // 公文类型
			paramXml += 			"<urn:EXT>" + oaExt + "</urn:EXT>"; // 预留字段
			paramXml += 		"</urn:RESAVEGW>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(flowOperPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "RESAVEGWReturn"); // OA系统接收Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断下一环节Xml是否为空
					System.out.println(resultXml);
					/** Document doc = DocumentHelper.parseText(resultXml);
					Element root = doc.getRootElement(); **/
				}
			}
		} catch (Exception e) {
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、taskAccept；报错内容、" + e.getMessage());
			return Result.error(CodeMsg.SERVER_ERROR.fillArgs(e.getMessage()));
		}
		return null;
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 下午2:09:18
	 * 描述 : OA系统“提交”
	 * 包名 : com.chinacoal.microservice.impl.service
	 * 方法名 : taskSubmit
	 *  Result<Object>  
	 *  @throws
	 */
	public Result<Object> taskSubmit(Map<String, Object> param) throws Exception {
		String oaUnId = (String) param.get("oaUnId"); // 待办ID
		if (StringUtils.isBlank(oaUnId)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“待办ID”参数！"));
		}
		String oaDataPath = (String) param.get("oaDataPath"); // 数据库路径
		if (StringUtils.isBlank(oaDataPath)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“数据库路径”参数！"));
		}
		String oaUserAccount = (String) param.get("oaUserAccount"); // 用户账号
		if (StringUtils.isBlank(oaUserAccount)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“用户账号”参数！"));
		}
		String oaNextuser = (String) param.get("oaNextuser"); // 下一处理人
		if (StringUtils.isBlank(oaNextuser)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“下一处理人”参数！"));
		}
		String oaOpin = (String) param.get("oaOpin"); // 审批意见
		String oaFlownum = (String) param.get("oaFlownum"); // 分支标识
		if (StringUtils.isBlank(oaFlownum)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“分支标识”参数！"));
		}
		String oaExtension = (String) param.get("oaExtension"); // 按钮标识
		if (StringUtils.isBlank(oaExtension)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“按钮标识”参数！"));
		}
		String oaMail = (String) param.get("oaMail"); // 是否邮件通知
		if (StringUtils.isBlank(oaMail)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“是否邮件通知”参数！"));
		}
		String oaSms = (String) param.get("oaSms"); // 是否短信通知
		if (StringUtils.isBlank(oaSms)) {
			return Result.error(CodeMsg.BIND_ERROR.fillArgs("缺少“是否短信通知”参数！"));
		}
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:SUBMITFLOW>";
			paramXml += 			"<urn:UNID>" + oaUnId + "</urn:UNID>"; // 待办ID
			paramXml += 			"<urn:DATAPATH>" + oaDataPath + "</urn:DATAPATH>"; // 数据库路径
			paramXml += 			"<urn:CURUSER>" + oaUserAccount + "</urn:CURUSER>"; // 用户账号
			paramXml += 			"<urn:NEXTUSER>" + oaNextuser + "</urn:NEXTUSER>"; // 下一处理人
			paramXml += 			"<urn:OPIN>" + oaOpin + "</urn:OPIN>"; // 审批意见
			paramXml += 			"<urn:FLOWNUM>" + oaFlownum + "</urn:FLOWNUM>"; // 分支标识
			paramXml += 			"<urn:EXTENSION>" + oaExtension + "</urn:EXTENSION>"; // 按钮标识
			paramXml += 			"<urn:MAIL>" + oaMail + "</urn:MAIL>"; // 是否邮件通知
			paramXml += 			"<urn:SMS>" + oaSms + "</urn:SMS>"; // 是否短信通知
			paramXml += 		"</urn:HUIQIANSELSECT>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(flowOperPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "SUBMITFLOWReturn"); // OA系统提交Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断下一环节Xml是否为空
					System.out.println(resultXml);
					/** Document doc = DocumentHelper.parseText(resultXml);
					Element root = doc.getRootElement(); **/
				}
			}
		} catch (Exception e) {
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、taskSubmit；报错内容、" + e.getMessage());
			return Result.error(CodeMsg.SERVER_ERROR.fillArgs(e.getMessage()));
		}
		return null;
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 下午2:10:42
	 * 描述 : OA系统“下载附件”
	 * 包名 : com.chinacoal.microservice.impl.service
	 * 方法名 : downloadAttach
	 *  ResponseEntity<byte[]>
	 *  @throws
	 */
	public ResponseEntity<byte[]> downloadAttach(String fileName, String filePath, String sysTaskId)
			throws Exception {
		ResponseEntity <byte[]> result = null;
		try {
			String paramXml = "";
			paramXml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">";
			paramXml += "<soapenv:Header/>";
			paramXml += 	"<soapenv:Body>";
			paramXml += 		"<urn:getStream>";
			paramXml += 			"<urn:url>" + URLDecoder.decode(filePath, "UTF-8") + "</urn:url>"; // 附件地址
			paramXml += 		"</urn:getStream>";
			paramXml +=		"</soapenv:Body>";
			paramXml += "</soapenv:Envelope>";
			Map<String, String> headParam = new HashMap<String, String>();
			headParam.put("SOAPAction", "");
			headParam.put("Content-Type", "text/xml;charset=UTF-8");
			String soapXml = Tool.invokeInterfaceWebService(downAttachPath, paramXml, headParam, HttpMethod.POST);
			if (StringUtils.isNoneBlank(soapXml)) { // 判断SoapXml是否为空
				String resultXml = analysisSoapXml(soapXml, "getStreamReturn"); // OA系统下载附件Xml
				if (StringUtils.isNoneBlank(resultXml)) { // 判断OA系统附件Xml是否为空
					byte[] b = Base64.getDecoder().decode(resultXml); // 附件byte[]
					fileName = URLDecoder.decode(fileName, "UTF-8"); // URL解码
					HttpHeaders headers = new HttpHeaders(); // 设置响应头
					headers.add("Content-Type", "application/force-download");
					headers.add("Content-Type", "application/vnd.ms-excel");
					headers.add("Content-Disposition",
							"attachment;fileName=\"" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + "\"");
					result = ResponseEntity.ok().headers(headers).contentLength(b.length)
							.contentType(MediaType.parseMediaType("application/octet-stream")).body(b);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("报错日志：类名称、OaTaskServiceImpl；方法名、downloadAttach；报错内容、" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * 
	 * 创建人 : yn
	 * 创建时间 : 2020年5月12日 上午10:10:28
	 * 描述 : 解析soapXml
	 * 包名 : com.chinacoal.microservice.impl.util
	 * 方法名 : analysisSoapXml
	 *  String  
	 *  @throws
	 */
	public static String analysisSoapXml(String soapXml, String key) {
		try {
			Document doc = DocumentHelper.parseText(soapXml);
			Element root = doc.getRootElement(); // 获取根节点
			Element elm = root.element("Body");
			// 获取Body下的所有子节点
			List<?> bodyChild = elm.elements();
			// 遍历所有的Body当前节点
			for (Iterator<?> it = bodyChild.iterator(); it.hasNext();) {
				Element elm1 = (Element) it.next();
				List<?> responseChild = elm1.elements();
				// 遍历Response节点下的所有节点
				for (Iterator<?> it1 = responseChild.iterator(); it1.hasNext();) {
					Element elm2 = (Element) it1.next();
					// 取得最后节点下的名称和值
					if (key.equals(elm2.getName())) {
						return elm2.getText();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
